package cn.shyouchi.wuguanjia.android.ui.patrol.begin;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.Global;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.patrol.PatrolApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.components.storage.UserStorage;
import cn.shyouchi.wuguanjia.android.event.PatrolChangeEvent;
import cn.shyouchi.wuguanjia.android.event.PatrolInfoChangeEvent;
import cn.shyouchi.wuguanjia.android.impl.ModuleItemClickListener;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.ui.event.checknum.CheckNumLineEntity;
import cn.shyouchi.wuguanjia.android.widget.GlideCircleTransform;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;


public class BeginPatrolActivity extends BaseActivity implements ModuleItemClickListener<PatrolRingDesEntity.ResultMessageBean> {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.img_header)
    ImageView mImgHeader;
    @BindView(R.id.tv_person_name)
    TextView mTvPersonName;
    @BindView(R.id.rv_ring_des)
    RecyclerView mRvRingDes;

    PatrolRingDesAdapter mPatrolRingDesAdapter;
    List<PatrolRingDesEntity.ResultMessageBean> mPatrolRingDesEntities = new ArrayList<>();
    UserStorage mUserStorage;

    PatrolApi mPatrolApi;

    CheckNumLineEntity.ResultMessageBean mPatrolLine;
    PatrolRingDesEntity.ResultMessageBean model;


    @Override
    public int initContentView() {
        return R.layout.activity_begin_patrol;
    }

    @Override
    public void initInjector() {
        mUserStorage = new UserStorage(mContext);
        mPatrolApi = new PatrolApi(mContext);
        mPatrolLine = getIntent().getParcelableExtra("line");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());
        EventBus.getDefault().register(this);

//        mPatrolRingDesEntities.add(new PatrolRingDesEntity());
//        mPatrolRingDesEntities.add(new PatrolRingDesEntity());
//        mPatrolRingDesEntities.add(new PatrolRingDesEntity());
//        mPatrolRingDesEntities.add(new PatrolRingDesEntity());
//        mPatrolRingDesEntities.add(new PatrolRingDesEntity());
//        mPatrolRingDesEntities.add(new PatrolRingDesEntity());
//        mPatrolRingDesEntities.add(new PatrolRingDesEntity());


        mPatrolRingDesAdapter = new PatrolRingDesAdapter(mPatrolRingDesEntities, this);
        mRvRingDes.setLayoutManager(new LinearLayoutManager(mContext));
        mRvRingDes.setAdapter(mPatrolRingDesAdapter);

        bindPersonDetail();

        fetchPatrolLineRing();
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    private void bindPersonDetail() {
        mTvPersonName.setText(mUserStorage.getUserData().getName());
        loadHead(Global.BASE_URL + mUserStorage.getUserData().getAvatar());

    }

    private void loadHead(String path) {
        Glide.with(mContext)
                .load(path)
                .placeholder(R.mipmap.ic_default_header)
                .transform(new GlideCircleTransform(mContext))
                .into(mImgHeader);
    }


    @Override
    public void moduleItemClick(PatrolRingDesEntity.ResultMessageBean model) {
        this.model = model;
        Intent intent = new Intent(mContext, PatrolRingDetailActivity.class);
        intent.putExtra("model", model);
        startActivity(intent);

    }

    @Override
    public void moduleItemLongClick(PatrolRingDesEntity.ResultMessageBean model) {

    }

    @Override
    public void onDel(PatrolRingDesEntity.ResultMessageBean model) {

    }

    private void fetchPatrolLineRing() {
        mPatrolApi.getPatrolLineRings(String.valueOf(mPatrolLine.getId()))
                .subscribeOn(Schedulers.io())
                .map(new Func1<PatrolRingDesEntity, List<PatrolRingDesEntity.ResultMessageBean>>() {
                    @Override
                    public List<PatrolRingDesEntity.ResultMessageBean> call(PatrolRingDesEntity entity) {
                        return entity.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<PatrolRingDesEntity.ResultMessageBean>>(mContext) {
                    @Override
                    public void onNext(List<PatrolRingDesEntity.ResultMessageBean> resultMessageBeen) {
                        mPatrolRingDesEntities.clear();
                        mPatrolRingDesEntities.addAll(resultMessageBeen);
                        mPatrolRingDesAdapter.notifyDataSetChanged();
                        if (model != null) {
                            for (PatrolRingDesEntity.ResultMessageBean b : resultMessageBeen) {
                                if (b.getId() == model.getId()) {
                                    EventBus.getDefault().post(new PatrolInfoChangeEvent(b));
                                    return;
                                }
                            }

                        }

                    }
                });
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPatrolChange(PatrolChangeEvent event) {
        fetchPatrolLineRing();
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
